package cn.edu.zjgsu.ito.shiro;

import java.util.List;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.zjgsu.ito.dao.UserLoginMapper;
import cn.edu.zjgsu.ito.model.UserLogin;
import cn.edu.zjgsu.ito.model.UserLoginExample;
import cn.edu.zjgsu.ito.service.UserLoginServiceI;

public class MyRealm extends AuthorizingRealm {

	@Autowired
	private UserLoginServiceI userLoginService;

	@Autowired
	private UserLoginMapper userLoginMapper;

	/**
	 * 用于权限的认证
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		// TODO Auto-generated method stub

		return null;
	}

	/**
	 * 执行登录验证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub

		String username = token.getPrincipal().toString();

		UserLoginExample example = new UserLoginExample();
		example.or().andUsernameEqualTo(username);
		List<UserLogin> userLogins = userLoginMapper.selectByExample(example);
		if (userLogins.size() > 0) {
			AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
					userLogins.get(0).getUsername(), userLogins.get(0)
							.getPassword(), "a");
			return authenticationInfo;
		}
		return null;
	}

}
